/**
*      author: Y_Yao
*      created: 2024-10-14 10:19:41
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 9;
priority_queue<LL,vector<LL>,greater<>> heap;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n = 0;
    cin >> n;

    for(int i = 0;i < n;i++)
    {
        LL in = 0;
        cin >> in;

        heap.push(in);
    }

    LL ret = 0;
    while(heap.size() > 1)
    {
        LL a = heap.top();
        heap.pop();
        LL b = 0;
        if(!heap.empty())
        {
            b = heap.top();
            heap.pop();
        }
        ret += a + b;
        heap.push(a + b);
    }

    cout << ret << endl;
    return 0;
}